<?php defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
/**
 * Title          ads
 *
 * @package        LMJ-futureid
 * Location        application/frontend/libraries/mobile
 *
 * @author         azeem 
 * @copyright      Cyber L-M-J
 * Description     this is for the ads(offers promotion/deals)
 *
 **/

class merchant_lib {
    //put your code here
    
    
       /**
	 * CodeIgniter global
	 *
	 * @var string
	 **/
	protected $ci;
	
	/**
	 * @var array(status=>1-success/0-fail, message=>string)
	 * this shoould be returned as jeson encoded variable using echo
	 *
	 **/
	public $return;
	public $key;
	protected $media_web_path;
	
    function __construct()
    {
    	$this->ci = & get_instance();
    	$this->ci->load->helper('password_decrypt_helper');
		$this->ci->load->model('merchant');
		$this->ci->load->library('photo/Album');
		$this->ci->load->library('photo/Photo');
    	$this->key='zkrowzppaekamew!';
    	$this->media_web_path = $this->ci->config->item('media_web_path');
    	$this->return['status']=0;
    	$this->return['message']='invalied request';
    }
    
    /**
     *  @author: lucilsandaruwan@gmail.com
     **/
    public function login($string)
    {
    	$email=@$string->{'email'};
    	$device_id=@$string->{'device_id'};
    	$password=@$string->{'password'};
    	$email=decrypt_password($email,$this->key);
    	$password=decrypt_password($password,$this->key);
    	$merchant=array('Email'=>$email,'Password'=>md5($password),'IsActive'=>1);
    	
    	if($email && $device_id && $password )
    	{
    		$results=$this->ci->merchant->get_merchnt_details($merchant);
    		if($results)
    		{
	    		$this->return['status']=1;
	    		$this->return['message']=current($results);
    		}
    		else
    		{
    			$this->return['message']="invalied user";
    		}
    	}
    	echo json_encode($this->return, true);
    }
    
    public function signup($string)
    {
    	$city=@$string->{'city'};
    	$password=@$string->{'password'};
    	$country_code=@$string->{'country_code'};
    	$street=@$string->{'street'};
    	$contact_number=@$string->{'contact_number'};
    	$state=@$string->{'state'};
    	$email=@$string->{'email'};
    	$country_name=@$string->{'country_name'};
    	$last_name=@$string->{'last_name'};
    	$business_phone=@$string->{'business_phone'};
    	$business_name=@$string->{'business_name'};
    	$first_name=@$string->{'first_name'};
    	$business_email=@$string->{'business_email'};
    	$currency_id=@$string->{'currency_id'};
    	
    	$business=array(
    			'BusinessName'=>$business_name,
    			'BusinessPhone'=>$business_phone,
    			'CityName'=>$city,
    			'CountryCode'=>$country_code,
    			'StreetName'=>$street,
    			'BusinessPhone'=>$business_phone,
    			'CountryName'=>$country_name,
    			'BusinessEmail'=>$business_email,
    			'CurrencyID'=>$currency_id,
    			'IsActive'=>1,
    			'StateName'=>$state);
    	
    	$client=array(
    			'Password'=>md5(decrypt_password($password,$this->key)),
    			'Email'=>decrypt_password($email,$this->key),
    			'LastName'=>$last_name,
    			'FirstName'=>$first_name,
    			'CreatedDate'=>time(),
    			'IsActive'=>1,
    			'ContactNumber'=>$contact_number);
    	
    	$client_data=array('Email'=>decrypt_password($email,$this->key),'IsActive'=>1);
    	
    	//check wether the user is already registerd
    	$results=$this->ci->merchant->get_merchnt_details($client_data);
    	if(is_array($results) && count($results)>0)
    	{
    		$this->return['message']="Email address allready exist";
    		echo json_encode($this->return, true);
    		die;
    	}
    	//adding newe record to bussiness tables
    	if($password && $email && $business_name)
    	{
    		$business_id=$this->ci->merchant->create_business($business);
    		if($business_id)
    		{
    			$client['BussinessID']=$business_id;
    			$client_id=$this->ci->merchant->create_client($client);
    			if($client_id)
    			{
    				$this->return['status']=1;
    				$this->return['message']=array('client_id'=>$client_id,'business_id'=>$business_id);
    			}
    			else 
    			{
    				$this->return['message']="there is an error on server";
    			}
    		}
    		else 
    		{
    			$this->return['message']="there is an error on server";
    		}
    	}
    	
    	echo json_encode($this->return, true);
    }
    
    public function get_currency()
    {
    	$this->ci->load->model('currency');
    	$results=$this->ci->currency->get_currencies();
    	if(is_array($results) && count($results))
    	{
    		$this->return['status']=1;
    		$this->return['message']=$results;
    	}
    	else 
    	{
    		$this->return['message']="Sory there are no currencies added to db now";
    	}
    	echo json_encode($this->return, true);
    }
    
    public function add_shop_attribute_values($string)
    {
    	$section=@$string->{'section'};
    	$lists=@$string->{'list'};
    	$client_id=@$string->{'client_id'};
    	$business_id=@$string->{'business_id'};
    	$order_id=@$string->{'order'};
    	$business=array('AttributeLabel'=>$section,'Order'=>$order_id,'BusinessID'=>$business_id,'IsActive'=>1,'CreatedDate'=>time());
    	if(isset($client_id) && isset($business_id))
    	{
    		$attr_id=$this->ci->merchant->add_business_attributes($business);
    		if($attr_id)
    		{
    			$values=array();
    			$value=array('AttributeID'=>$attr_id,'IsActive'=>1);
    			if(count($lists)>0)
    			{
    				foreach($lists as $list)
    				{
    					$value['ValueLabel']=$list->label;
    					$value['Order']=$list->order;
    					$value['CreatedDate']=time();
    					$values[]=$value;
    				}
    				$this->ci->merchant->add_business_attributes_values_as_batch($values);
    			}
    			$this->return['status']=1;
    			$this->return['message']="attribute added successfully";
    		}
    		else 
    		{
    			$this->return['message']="server error!";
    		}
    		
    	}
    	
    	
    	echo json_encode($this->return, true);
    	
    }
    
    public function get_shop_attributes($string)
    {
    	$client_id=@$string->{'client_id'};
    	$business_id=@$string->{'business_id'};
    	$attr_list=$this->ci->merchant->get_business_attributes(array('BusinessID'=>$business_id));
    	$attr_res_arr=array();
    	if(!empty($attr_list))
    	{
    		foreach($attr_list as $attr)
    		{
    			$attr_list_values=$this->ci->merchant->get_business_attributes_values(array('AttributeID'=>$attr['AttributeID']));
    			if(!empty($attr_list_values))
    			{
    				$attr['values']=$attr_list_values;
    			}
    			$attr_res_arr[]=$attr;	
    		}
    	}
    	if(!empty($attr_res_arr))
    	{
    		$this->return['status']=1;
    		$this->return['message']=$attr_res_arr;
    	}
    	else
    	{
    		$this->return['message']="You have not set attributes yet";
    	}
    	echo json_encode($this->return, true);
    }
    
    public function get_merchant_billboard_images($string){
    	$business_id = $business_id=@$string->{'business_id'};
    	$billboard_album = $this->ci->album->get_albums(array('BusinessID'=>$business_id,'AlbumTypeID'=>2));
    	if(!empty($billboard_album)){
    		$billboard_album = current($billboard_album);
    		$albums_photos = $this->ci->photo->get_album_photos($billboard_album['AlbumID'],10);
    		foreach ($albums_photos as &$album_photo){
    			$album_photo['ImageUrl'] = $this->media_web_path.'photos/fine_cropped/200/200/business/albums/'.md5($business_id).'/billboard/'.$album_photo['ImageUrl'];
    		}
    		$this->return['status'] = 1;
    		$this->return['message'] = array('album_id'=>$billboard_album['AlbumID'],'billboard_images'=>$albums_photos);
    	}else{
    		$this->return['status'] = 0;
    		$this->return['message'] = 'Sorry can\'t find album';
    	}
    	echo json_encode($this->return, true);
    	exit;
    }
    
    public function delete_merchant_billboard_images($string){
    	$business_id = $business_id=@$string->{'business_id'};
    	$album_id = $business_id=@$string->{'album_id'};
    	$album_photo_id = $business_id=@$string->{'album_photo_id'};
    	if($$business_id && $album_id && $this->ci->photo->delete_album_photo($business_id,$album_id,$album_photo_id)){
    		$this->return['status'] = 1;
    		$this->return['message'] = 'Billboard image delete successfuly';
    	}else{
    		$this->return['status'] = 0;
    		$this->return['message'] = 'Sorry can\'t find album';
    	}
    	echo json_encode($this->return, true);
    	exit;
    }
}